package com.hooper.controller;

import com.hooper.anno.PreAuthorize;
import com.hooper.common.PageResult;
import com.hooper.common.Result;
import com.hooper.model.system.SysRole;
import com.hooper.model.vo.RoleInsertVo;
import com.hooper.model.vo.SysRoleQueryVo;
import com.hooper.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/03/28/20:14
 */
@Api(tags = "角色管理")
@Slf4j
@RestController
@RequestMapping("/admin/system/role")
public class SysRoleController {

    @Resource
    private SysRoleService roleService;

    @PreAuthorize("bnt.sysRole.list")
    @ApiOperation("获取所有角色")
    @GetMapping("/list")
    public Result<List<SysRole>> getAll() {
        return Result.ok(roleService.getAllRoles());
    }

    @ApiOperation("分页查询角色")
    @PreAuthorize("bnt.sysRole.list")
    @PostMapping("page/{currentPage}/{pageSize}")
    public Result<PageResult<SysRole>> page(@PathVariable Integer currentPage,
                                            @PathVariable Integer pageSize,
                                            @RequestBody(required = false) SysRoleQueryVo queryVo) {
        log.info("接收到请求，参数：currentPage={},pageSize={},queryVo={}", currentPage, pageSize, queryVo);
        return Result.ok(roleService.pageByConditions(currentPage, pageSize, queryVo));
    }

    @ApiOperation("更新角色")
    @PreAuthorize("bnt.sysRole.update")
    @PutMapping("update")
    public Result<Void> update(@RequestBody SysRole newRole) {
        roleService.updateRoleById(newRole);
        return Result.ok();
    }

    @ApiOperation("新增角色")
    @PreAuthorize("bnt.sysRole.add")
    @PostMapping("save")
    public Result<Void> save(@RequestBody RoleInsertVo roleInsertVo) {
        roleService.saveRole(roleInsertVo);
        return Result.ok();
    }

    @PreAuthorize("bnt.sysRole.remove")
    @ApiOperation("删除角色")
    @DeleteMapping("/{id}")
    public Result<Void> delById(@PathVariable Long id) {
        roleService.delRoleById(id);
        return Result.ok();
    }


    @PreAuthorize("bnt.sysRole.remove")
    @ApiOperation("批量删除角色")
    @DeleteMapping("/del-batch")
    public Result<Void> delBatch(@RequestBody List<Long> ids) {
        log.info("接收到请求，参数：ids={}", ids);
        roleService.delBatchByIds(ids);
        return Result.ok();
    }

    @ApiOperation("通过id获取角色")
    @PreAuthorize("bnt.sysRole.list")
    @GetMapping("/{id}")
    public Result<SysRole> getById(@PathVariable Long id) {
        return Result.ok(roleService.getById(id));
    }
}
